package cn.life.common.utils.PDF;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * @Description:
 * @Author 李佳明
 * @Date 2018-10-09 11:01
 */
public class PdfUtil {
    /*private static Font headfont ;// 设置字体大小
    private static Font keyfont;// 设置字体大小
    private static Font textfont;// 设置字体大小

    static{
        BaseFont bfChinese;
        try {
            bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            headfont = new Font(bfChinese, 12, Font.BOLD,BaseColor.BLACK);// 设置字体大小
            keyfont = new Font(bfChinese, 10, Font.BOLD,new BaseColor(108,0,108));// 设置字体大小
            textfont = new Font(bfChinese, 8, Font.NORMAL,BaseColor.BLACK);// 设置字体大小
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /**
     * @Description:    设置标题
     *
     * @param headName 标题名称，type 标题格式（居左，居中，居右），afterSpacing 后间距
     * @return:
     *
     * @Author 李佳明
     * @Date 2018-10-09 下午 1:42
     */
    public Paragraph getHead(String headName,Integer type,Integer afterSpacing) throws IOException, DocumentException {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font FontChinese18 = new Font(bfChinese, 18,com.itextpdf.text.Font.BOLD);
        Paragraph pg_bt = new Paragraph(headName, FontChinese18);//标题
        if(type == null){
            type = Element.ALIGN_CENTER;
        }
        pg_bt.setAlignment(type);
        if(afterSpacing == null){
            afterSpacing = 50;
        }
        pg_bt.setSpacingAfter(afterSpacing);
        return pg_bt;
    }

    /**
     * @Description:    增加线条
     *
     * @param lineWidth 线条宽度， percentage 百分比， color 颜色
     * @return:
     *
     * @Author 李佳明
     * @Date 2018-10-09 下午 1:49
     */
    public Paragraph getLineStyle(float lineWidth,float percentage,BaseColor color){
        Paragraph linePar = new Paragraph("");
        //加入一条蓝线
        linePar.add(new Chunk(new LineSeparator(lineWidth, percentage, color, Element.ALIGN_CENTER, 0)));
        return linePar;
    }

    /**
     * @Description:    增加线条
     *
     * @param lineWidth 线条宽度， percentage 百分比， color 颜色
     * @return:
     *
     * @Author 李佳明
     * @Date 2018-10-09 下午 1:49
     */
    public Paragraph getLineStyle(float lineWidth,float percentage,BaseColor color,Integer type){
        Paragraph linePar = new Paragraph("");
        //加入一条蓝线
        linePar.add(new Chunk(new LineSeparator(lineWidth, percentage, color, type, 0)));
        return linePar;
    }
    
    /**
     * @Description:    获取段落
     *
     * @param paragraphName 段落名称，fontChinese11Normal 段落样式，titleindentationleft 左侧缩进长度
     * @return: 
     *
     * @Author 李佳明
     * @Date 2018-10-09 下午 2:04 
     */
    public Paragraph getParagraph(String paragraphName, Font fontChinese11Normal, float titleindentationleft) {
        Paragraph paragraph = new Paragraph(paragraphName, fontChinese11Normal);
        // 段落缩进
        paragraph.setIndentationLeft(titleindentationleft);
        return paragraph;
    }
/*

    //表格标题
    public static PdfPCell createHeadCell(String value){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(15);
        cell.setHorizontalAlignment(15);
        cell.setColspan(9);
        cell.setPhrase(new Phrase(value,headfont));
        cell.setPadding(10.0f);
        cell.setBorder(0);
        cell.setPaddingTop(15.0f);
        cell.setPaddingBottom(8.0f);
        return cell;
    }

    //表格表头
    public static  PdfPCell createTitleCell(String value){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, keyfont));
        cell.setBackgroundColor(new BaseColor(0, 204, 255));
        return cell;
    }

    //表格内容
    public static PdfPCell createCell(String value){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value,textfont));
        cell.setBackgroundColor(new BaseColor(255, 255, 153));
        return cell;
    }


    //生成表格
    public static PdfPTable createTable(int colNumber){
        PdfPTable table = new PdfPTable(colNumber);
        try{
            table.setLockedWidth(true);
            table.setTotalWidth(500);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        }catch(Exception e){
            e.printStackTrace();
        }
        return table;
    }

    public static  void addImage(String input,String output) throws Exception{
        BufferedOutputStream out = new BufferedOutputStream(
                new FileOutputStream(new File(output)));
        PdfReader reader = new PdfReader(input);
        PdfStamper stamper = new PdfStamper(reader, out);
        addWatermark(stamper,"我是测试水印文字");
        int total = reader.getNumberOfPages();
        try {
            Image image = Image.getInstance("d:/1.png");
            image.setAbsolutePosition(350, 100); // set the first background image of the absolute
            image.scaleToFit(120, 120);
            PdfContentByte content= stamper.getOverContent(total);// 在内容上方加水印
            content.addImage(image);
        }catch (Exception e){
            System.out.println("123");
        }

        stamper.close();
        reader.close();
    }

    public static void addWatermark(PdfStamper pdfStamper, String waterMarkName) throws Exception {
        PdfContentByte content = null;
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        Rectangle pageRect = null;
        PdfGState gs = new PdfGState();
        try {
            if (base == null || pdfStamper == null) {
                return;
            }
            // 设置透明度为0.4
            gs.setFillOpacity(0.4f);
            gs.setStrokeOpacity(0.4f);
            int toPage = pdfStamper.getReader().getNumberOfPages();
            for (int i = 1; i <= toPage; i++) {
                pageRect = pdfStamper.getReader().getPageSizeWithRotation(i);
                // 计算水印X,Y坐标
                float x = pageRect.getWidth() / 2;
                float y = pageRect.getHeight() / 2;
                // 获得PDF最顶层
                content = pdfStamper.getOverContent(i);
                content.saveState();
                // set Transparency
                content.setGState(gs);
                content.beginText();
                content.setColorFill(BaseColor.GRAY);
                content.setFontAndSize(base, 30);
                // 水印文字成45度角倾斜
                content.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x,
                        y, 45);
                content.endText();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
*/


}
